<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Hugo 0.82.0" />


<title>Deploy shiny - 宇飞的世界</title>
<meta property="og:title" content="Deploy shiny - 宇飞的世界">


  <link href='/favicon.ico' rel='icon' type='image/x-icon'/>



  








<link href='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css' rel='stylesheet' type='text/css' />



<link rel="stylesheet" href="/css/fonts.css" media="all">
<link rel="stylesheet" href="/css/main.css" media="all">



  </head>
  <body>
    <div class="wrapper">
      <header class="header">
        <nav class="nav">
  <a href="/" class="nav-logo">
    <img src="/images/logo.png"
         width="50"
         height="50"
         alt="Logo">
  </a>

  <ul class="nav-links">
    
    <li><a href="/about/">About</a></li>
    
    <li><a href="/categories/">Categories</a></li>
    
    <li><a href="/tags/">Tags</a></li>
    
    <li><a href="http://www.zhongyufei.com/Rbook/_book">R study</a></li>
    
  </ul>
</nav>

      </header>


<main class="content" role="main">

  <article class="article">
    
    <span class="article-duration">2 min read</span>
    

    <h1 class="article-title">Deploy shiny</h1>

    
    <span class="article-date">2020-07-29</span>
    

    <div class="article-content">
      <h3 id="安装">安装</h3>
<p>官网资料：<a href="https://rstudio.com/products/shiny/download-server/">https://rstudio.com/products/shiny/download-server/</a></p>
<ol>
<li>第一步安装R</li>
</ol>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-R" data-lang="R">sudo yum install R
</code></pre></div><p>centos7 上可直接用上面命令安装R,但是在centos 8 安装会无法成功。因为R软件包没有被包含在 CentOS 8 的核心软件源中。我们需要从 EPEL 软件源中安装 R</p>
<p>启用 EPEL 和 PowerTools 软件源</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">sudo dnf install epel-release
sudo dnf config-manager --set-enabled PowerTools
</code></pre></div><p>安装R输入：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">sudo yum install R
</code></pre></div><p>查看R版本 <code> R --version</code></p>
<ol start="2">
<li>第二步 安装shiny package</li>
</ol>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-R" data-lang="R">Rsudo su <span style="color:#f92672">-</span> \
<span style="color:#f92672">-</span>c <span style="color:#e6db74">&#34;R -e \&#34;install.packages(&#39;shiny&#39;, repos=&#39;https://cran.rstudio.com/&#39;)\&#34;&#34;</span>
</code></pre></div><p>或者近入R后<code>install.package('shiny')</code> 安装</p>
<ol start="3">
<li>第三步 下载安装shiny</li>
</ol>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-R" data-lang="R">wget https<span style="color:#f92672">://</span>download3.rstudio.org<span style="color:#f92672">/</span>centos6.3<span style="color:#f92672">/</span>x86_64<span style="color:#f92672">/</span>shiny<span style="color:#f92672">-</span>server<span style="color:#ae81ff">-1.5.14.948</span><span style="color:#f92672">-</span>x86_64.rpm

sudo yum install <span style="color:#f92672">--</span>nogpgcheck shiny<span style="color:#f92672">-</span>server<span style="color:#ae81ff">-1.5.14.948</span><span style="color:#f92672">-</span>x86_64.rpm
</code></pre></div><ol start="4">
<li>第四步 测试是否安装成功</li>
</ol>
<p><a href="http://ip:3838">http://ip:3838</a></p>
<p>ip地址如果是本机,可以换成localhost,或者是本机ip地址。如果是公网像云服务器,需要开放3838的端口后再测试。</p>
<ol start="5">
<li>查看shiny-server 状态</li>
</ol>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">sudo systemctl status shiny-server
</code></pre></div><h3 id="部署">部署</h3>
<h4 id="部署在个人服务器上">部署在个人服务器上</h4>
<ul>
<li>本地文件上传</li>
</ul>
<p>app.R</p>
<ul>
<li>修改shiny 配置文件</li>
</ul>
<p>进入 <code>sudo vim /etc/shiny-server/shiny-server.conf</code></p>
<p>添加</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-R" data-lang="R">location <span style="color:#f92672">/</span>demo1{ <span style="color:#75715e">#/demo1 访问app.R的网址后缀</span>
         app_dir <span style="color:#f92672">/</span>home<span style="color:#f92672">/</span>shiny<span style="color:#f92672">/</span>shinyapp<span style="color:#f92672">/</span>demo1; <span style="color:#75715e">#app.R所在文件夹</span>
        log_dir <span style="color:#f92672">/</span>var<span style="color:#f92672">/</span>log<span style="color:#f92672">/</span>shiny<span style="color:#f92672">-</span>server<span style="color:#f92672">/</span>demo1;<span style="color:#75715e">#app.R日志所在文件夹</span>
}
<span style="color:#75715e"># 添加后</span>

<span style="color:#75715e"># Instruct Shiny Server to run applications as the user &#34;shiny&#34;</span>
run_as shiny; <span style="color:#75715e"># 依赖的包需要再shiny用户下安装</span>

<span style="color:#75715e"># Define a server that listens on port 3838</span>
server {
  listen <span style="color:#ae81ff">3838</span>;

  <span style="color:#75715e"># Define a location at the base URL</span>
  location <span style="color:#f92672">/</span> {

    <span style="color:#75715e"># Host the directory of Shiny Apps stored in this directory</span>
    site_dir <span style="color:#f92672">/</span>srv<span style="color:#f92672">/</span>shiny<span style="color:#f92672">-</span>server;

    <span style="color:#75715e"># Log all Shiny output to files in this directory</span>
    log_dir <span style="color:#f92672">/</span>var<span style="color:#f92672">/</span>log<span style="color:#f92672">/</span>shiny<span style="color:#f92672">-</span>server;

    <span style="color:#75715e"># When a user visits the base URL rather than a particular application,</span>
    <span style="color:#75715e"># an index of the applications available in this directory will be shown.</span>
    directory_index on;
  }

    location <span style="color:#f92672">/</span>demo1{                     <span style="color:#75715e">#/demo1 访问app.R的网址后缀</span>
        app_dir <span style="color:#f92672">/</span>home<span style="color:#f92672">/</span>shiny<span style="color:#f92672">/</span>shinyapp<span style="color:#f92672">/</span>demo1; <span style="color:#75715e">#app.R所在文件夹</span>
        log_dir <span style="color:#f92672">/</span>var<span style="color:#f92672">/</span>log<span style="color:#f92672">/</span>shiny<span style="color:#f92672">-</span>server<span style="color:#f92672">/</span>demo1;<span style="color:#75715e">#app.R日志所在文件夹</span>
}

}

</code></pre></div><ul>
<li>重启</li>
</ul>
<p><code>sudo systemctl restart shiny-server</code></p>
<h4 id="部署官方服务器">部署官方服务器</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-R" data-lang="R"><span style="color:#75715e">#本地安装rsconnect </span>
<span style="color:#a6e22e">install.packages</span>(<span style="color:#e6db74">&#39;rsconnect&#39;</span>)
<span style="color:#a6e22e">library</span>(rsconnect)

<span style="color:#75715e">#想要最新版本可以用以下命令安装该包</span>
<span style="color:#a6e22e">if</span>(<span style="color:#f92672">!</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#34;devtools&#34;</span>))
  <span style="color:#a6e22e">install.packages</span>(<span style="color:#e6db74">&#34;devtools&#34;</span>)
devtools<span style="color:#f92672">::</span><span style="color:#a6e22e">install_github</span>(<span style="color:#e6db74">&#34;rstudio/rsconnect&#34;</span>)

<span style="color:#75715e">#通过以下网址创建shinyapp.io账户</span>
https<span style="color:#f92672">://</span>www.shinyapps.io<span style="color:#f92672">/</span>

</code></pre></div><p>测试部署</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-R" data-lang="R"><span style="color:#75715e">#复制该代码到R窗口下运行</span>
rsconnect<span style="color:#f92672">::</span><span style="color:#a6e22e">setAccountInfo</span>(name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;genomenetwork&#39;</span>,
              token<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;869F3553609D3A17C74FA157528D58C8&#39;</span>,
              secret<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;&lt;SECRET&gt;&#39;</span>)
<span style="color:#75715e">#将工作目录设置到样本app的文件下</span>
<span style="color:#a6e22e">setwd</span>(<span style="color:#e6db74">&#34;~/Downloads/shiny-examples-master/001-hello&#34;</span>)
<span style="color:#75715e">#当运行以下命令成功运行程序时，目录设置正确</span>
shiny<span style="color:#f92672">::</span><span style="color:#a6e22e">runApp</span>()

<span style="color:#75715e"># 部署</span>
<span style="color:#75715e">#停止应用，运行以下代码部署app</span>
<span style="color:#a6e22e">library</span>(rsconnect)
<span style="color:#a6e22e">deployApp</span>()
<span style="color:#75715e">#部署完成自动弹出网页</span>
https<span style="color:#f92672">://</span>genomenetwork.shinyapps.io<span style="color:#f92672">/</span><span style="color:#ae81ff">001</span><span style="color:#f92672">-</span>hello<span style="color:#f92672">/</span>

</code></pre></div><h3 id="检查报错">检查报错</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#75715e">#应用启动失败，一般是依赖包问题，好好查看报错信息</span>
<span style="color:#75715e">#日志</span>
sudo cat /var/log/shiny-server/demo/
<span style="color:#75715e">#重启</span>
sudo systemctl restart shiny-server;
<span style="color:#75715e">#文件配置</span>
sudo vim /etc/shiny-server/shiny-server.conf
</code></pre></div><p>由于Shiny Server为了保证性能，所以非敏感性的错误日志被设置了自动清除，每当你出现了错误，
要去看日志定位问题的时候，这个日志就刚好被自动清除了。可做如下调整</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">sudo vi /etc/shiny-server/shiny-server.conf
run_as shiny;
access_log /var/log/shiny-server/access.log default;  <span style="color:#75715e"># 增加记录访问</span>
preserve_logs true;                                   <span style="color:#75715e"># 禁止自动清除日志</span>
<span style="color:#75715e"># Define a server that listens on port 3838</span>
server <span style="color:#f92672">{</span>
  listen 3838;
<span style="color:#75715e"># 省略</span>
<span style="color:#f92672">}</span>

</code></pre></div>
    </div>
  </article>

  


</main>

      <footer class="footer">
        <ul class="footer-links">
          <li>
            <a href="/index.xml" type="application/rss+xml" target="_blank">RSS feed</a>
          </li>
          <li>
            <a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备20035539号</a>
          </li>
        </ul>
      </footer>

    </div>
    



<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>



<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/r.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/yaml.min.js"></script>
<script>hljs.configure({languages: []}); hljs.initHighlightingOnLoad();</script>



    
<script src="/js/math-code.js"></script>
<script async src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>


    
  </body>
</html>

